package com.gwtgeo.ui.client.mvp.windowsmanager;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.user.client.ui.Widget;
import com.smartgwt.client.types.HeaderControls;
import com.smartgwt.client.widgets.HeaderControl;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.HasClickHandlers;

public abstract class WindowedView<T extends Widget> implements WindowedDisplay {

	private Map<String, HasClickHandlers> headers = new HashMap<String, HasClickHandlers>();

	protected Window window;

	protected T widget;

	public WindowedView() {
		super();
		widget = this.buildWidget();
		this.buildWindow();
	}

	private void buildHeaderControls() {
		headers.put(WindowedDisplay.CLOSE_HEADER, new HeaderControl(
				HeaderControl.CLOSE));
		headers.put(WindowedDisplay.MINIMIZE_HEADER, new HeaderControl(
				HeaderControl.MINIMIZE));
		this.addHeaderControls(headers);
	}

	@Override
	public Map<String, HasClickHandlers> getHeaderClicks() {
		return headers;
	}

	protected void addHeaderControls(Map<String, HasClickHandlers> headers) {
		window.setHeaderControls(HeaderControls.HEADER_LABEL, headers
				.get(WindowedDisplay.MINIMIZE_HEADER), headers
				.get(WindowedDisplay.CLOSE_HEADER));
	}

	protected void buildWindow() {
		this.window = new Window();
		this.window.setWidth(this.getWindowWidth());
		this.window.setHeight(this.getWindowHeight());
		this.window.setTitle(this.getWindowTitle());
		this.window.addItem(this.asWidget());
		this.buildHeaderControls();
	}

	@Override
	public final void show() {
		this.window.show();
	}

	@Override
	public final void hide() {
		this.window.hide();
	}

	protected abstract int getWindowWidth();

	protected abstract int getWindowHeight();

	protected abstract String getWindowTitle();

	protected abstract T buildWidget();

	public final Window asWindow() {
		return this.window;
	}

	@Override
	public final Widget asWidget() {
		return this.widget;
	}

}
